package com.xiaoshuidi.cloud.module.finance.service.collection.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoshuidi.cloud.framework.common.exception.ErrorCode;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xiaoshuidi.cloud.module.finance.controller.admin.collection.vo.FinanceCollectionConfigReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.collection.vo.FinanceCollectionConfigRespVO;
import com.xiaoshuidi.cloud.module.finance.convert.collection.FinanceCollectionConfigConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.collection.FinanceCollectionConfigDO;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.collection.FinanceCollectionConfigMapper;
import com.xiaoshuidi.cloud.module.finance.enums.finance.PayChannelTypeEnum;
import com.xiaoshuidi.cloud.module.finance.service.collection.FinanceCollectionConfigService;
import com.xiaoshuidi.cloud.module.system.api.apartment.ApartmentManageApi;
import com.xiaoshuidi.cloud.module.system.enums.apartment.ApartmentManageEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.regex.Pattern;

@Service
public class FinanceCollectionConfigServiceImpl implements FinanceCollectionConfigService {

    @Resource
    private FinanceCollectionConfigMapper financeCollectionConfigMapper;
    @Resource
    private ApartmentManageApi apartmentManageApi;
    /**
     * 获取配置信息
     *
     * @param apartmentId
     * @return
     */
    @Override
    public FinanceCollectionConfigRespVO get(Long apartmentId) {
        FinanceCollectionConfigDO financeCollectionConfigDO = financeCollectionConfigMapper.selectOne(FinanceCollectionConfigDO::getApartmentId, apartmentId);
        if (Objects.nonNull(financeCollectionConfigDO)){
            return FinanceCollectionConfigConvert.INSTANCE.convert(financeCollectionConfigDO);
        }
        return null;
    }

    @Override
    public FinanceCollectionConfigRespVO get(Long apartmentId, String payChannelType) {
        FinanceCollectionConfigDO financeCollectionConfigDO = financeCollectionConfigMapper.selectOne(new LambdaQueryWrapperX<FinanceCollectionConfigDO>()
                .eqIfPresent(FinanceCollectionConfigDO::getApartmentId,apartmentId)
                .eqIfPresent(FinanceCollectionConfigDO::getPayChannelType,payChannelType));
        return FinanceCollectionConfigConvert.INSTANCE.convert(financeCollectionConfigDO);
    }

    /**
     * 保存配置信息
     *
     * @param financeCollectionConfigReqVO
     */
    @Override
    public void save(FinanceCollectionConfigReqVO financeCollectionConfigReqVO) {
        //校验参数
        validate(financeCollectionConfigReqVO);
        FinanceCollectionConfigDO financeCollectionConfigDO = financeCollectionConfigMapper.selectOne(FinanceCollectionConfigDO::getApartmentId, financeCollectionConfigReqVO.getApartmentId());
        //不存在就初始化，存在做保存操作
        if (financeCollectionConfigDO == null){
            FinanceCollectionConfigDO convert = FinanceCollectionConfigConvert.INSTANCE.convert(financeCollectionConfigReqVO);
            financeCollectionConfigMapper.insert(convert);
            //更改门店配置状态
            apartmentManageApi.updateApartment(financeCollectionConfigReqVO.getApartmentId(), ApartmentManageEnum.COLLECTION_CONFIG.getCode());
            return;
        }
//        financeCollectionConfigDO.setCollectionName(financeCollectionConfigReqVO.getCollectionName());
//        financeCollectionConfigDO.setCollectionNo(financeCollectionConfigReqVO.getCollectionNo());
        FinanceCollectionConfigDO updateConfig = FinanceCollectionConfigConvert.INSTANCE.convert(financeCollectionConfigReqVO);
        updateConfig.setId(financeCollectionConfigDO.getId());
        financeCollectionConfigMapper.updateById(updateConfig);

    }

    private void validate(FinanceCollectionConfigReqVO financeCollectionConfigReqVO) {
        //校验收款名称是否为纯文字
        if(StringUtils.isNotBlank(financeCollectionConfigReqVO.getCollectionName()) && !Pattern.matches("^[\u4E00-\u9FA5]+$", financeCollectionConfigReqVO.getCollectionName())){
            throw new ServiceException(new ErrorCode(10021001, "收款名称必须为中文" ));
        }
        //校验收款账号是否为数字
//        if (StringUtils.isNotBlank(financeCollectionConfigReqVO.getCollectionNo()) && !financeCollectionConfigReqVO.getCollectionNo().matches("\\d+")){
//            throw new ServiceException(new ErrorCode(10021001, "收款账号必须为数字" ));
//        }
        // 默认通联
        if (StringUtils.isBlank(financeCollectionConfigReqVO.getPayChannelType())){
            financeCollectionConfigReqVO.setPayChannelType(PayChannelTypeEnum.TongLian.getCode());
        }
        // 通联字段校验
        if (StringUtils.equals(PayChannelTypeEnum.TongLian.getCode(), financeCollectionConfigReqVO.getPayChannelType())){
            if (StringUtils.isBlank(financeCollectionConfigReqVO.getCusId()) ||
                    StringUtils.isBlank(financeCollectionConfigReqVO.getAppId()) ||
                    StringUtils.isBlank(financeCollectionConfigReqVO.getCusPrivateKey()) ||
                    StringUtils.isBlank(financeCollectionConfigReqVO.getPltPubKey())){
                throw new ServiceException(new ErrorCode(10021001, "缺少必填字段" ));
            }
        }
    }


}
